/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.util.EventListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Browser version view class used in the settings dialog.
 * 
 *	@date 20.11.2008
 *	@author	Marcus Fuhrmeister
 */
public class BrowserVersionView extends JPanel implements View {

	private static final long serialVersionUID = 2021004833787146219L;
	private JLabel versionLabel;
	private JTextField versionTextField;
	private JLabel execLabel;
	private JTextField execTextField;
	private JButton execButton;
	private JButton deleteButton;
	private int browserID;

	/**
	 * Constructor to create a new browser version view.
	 * Sets the layout and calls to init the components.
	 * 
	 * @param width the width of the view
	 */
	public BrowserVersionView(final int width) {
		super(new FlowLayout(FlowLayout.LEADING));
		final Border b = Borders.DIALOG_BORDER;
		final Insets i = b.getBorderInsets(this);
		final int colWidth = (width - (i.left + i.right + View.SCROLLBARWIDTH)) / 4;
		final FormLayout layout = new FormLayout(colWidth + "px, " + colWidth + "px, " + colWidth + "px, " + colWidth + "px", "pref, 12px, pref, 12px, pref, 12px");
		this.setLayout(layout);
		final CellConstraints cc = new CellConstraints();
		this.initComponents(cc);
	}

	/**
	 * Inits the view components.
	 * 
	 * @param cc the CellConstraints
	 */
	private void initComponents(final CellConstraints cc) {
		// browser version
		versionLabel = new JLabel();
		this.add(versionLabel, cc.xy(1, 1));
		versionTextField = new JTextField();
		this.add(versionTextField, cc.xy(2, 1));
		// browser executable
		execLabel = new JLabel();
		this.add(execLabel, cc.xy(1, 3));
		execTextField = new JTextField();
		this.add(execTextField, cc.xyw(2, 3, 2));
		execButton = new JButton();
		this.add(execButton, cc.xy(4, 3));
		deleteButton = new JButton();
		this.add(deleteButton, cc.xy(4, 5));
	}

	/**
	 * Sets the browser id.
	 * 
	 * @param browserID the browser id
	 */
	public void setBrowserID(final int browserID) {
		this.browserID = browserID;
	}

	/**
	 * Returns the browser id.
	 * 
	 * @return the browser id
	 */
	public int getBrowserID() {
		return this.browserID;
	}

	/**
	 * Sets the browser version label value.
	 * 
	 * @param label the browser version label value
	 */
	public void setBrowserVersionProperties(final String label) {
		versionLabel.setText(label);
	}

	/**
	 * Sets the browser version text field value.
	 * 
	 * @param label the browser version text field value
	 */
	public void setBrowserVersion(final String version) {
		versionTextField.setText(version);
	}

	/**
	 * Returns the browser version text field value.
	 * 
	 * @return the browser version text field value
	 */
	public String getBrowserVersion() {
		return versionTextField.getText();
	}

	/**
	 * Sets the browser version executable label and button value.
	 * 
	 * @param label the browser version executable label
	 * @param button the browser version executable button
	 */
	public void setBrowserExecProperties(final String label, final String button) {
		execLabel.setText(label);
		execButton.setText(button);
		execButton.setActionCommand(button);
	}

	/**
	 * Sets the browser version executable text field value.
	 * 
	 * @param execPath the browser version executable text field value
	 */
	public void setBrowserExec(final String execPath) {
		execTextField.setText(execPath);
	}

	/**
	 * Returns the browser version executable text field value
	 * @return the browser version executable text field value
	 */
	public String getBrowserExec() {
		return execTextField.getText();
	}

	/**
	 * Sets the browser version view delete button text value.
	 * 
	 * @param button the browser version view delete button text value
	 */
	public void setBrowserDelete(final String button) {
		deleteButton.setText(button);
		deleteButton.setActionCommand(button);
	}

	/**
	 * Registers all listeners.
	 */
	public void registerListener(final EventListener el) {
		if (el instanceof ActionListener) {
			execButton.addActionListener((ActionListener) el);
			deleteButton.addActionListener((ActionListener) el);
		}
		if (el instanceof FocusListener) {
			versionTextField.addFocusListener((FocusListener) el);
			execTextField.addFocusListener((FocusListener) el);
		}
	}

	/**
	 * Enables/Disables the browser version view delete button
	 * 
	 * @param enable to enable/disable the button
	 * @return this BrowserVersionView
	 */
	public BrowserVersionView enableBrowserDelete(boolean enable) {
		deleteButton.setEnabled(enable);
		return this;
	}
}
